<?php
/*********
*    首页控制器
*/
namespace Home\Controller;
use Home\Controller\HomeController;
class IndexController extends HomeController {
	public function _initialize(){
		parent::_initialize();
	}
	public function index(){
		//进行失败订单的删除
		$shanchude=M('rob_orders ')->where(array('order_status'=>'-1'))->select();//查出为支付的订单
		foreach ($shanchude as $zk=>$zv){
			M('rob_goods_orders')->where(array('order_id'=>$zv['order_id']))->delete();
			M('rob_orders')->where(array('order_id'=>$zv['order_id']))->delete();
		}
		$result1=$this->weiXinLogin();//微信登录
		$order_id=I('order_id');
		$fx=I('fx');
		//获取用户信息
		//这里写一个获取用户信息的接口
		//dump($result1);die;
		if(empty($result1['openid'])){
			echo '网络错误!'; exit();
		}


		// dump($_SESSION['openid']);die;
		$where['openid'] = $result1['openid'];
		$user_info = M("Member")->where($where)->find();
		//如果账户在数据库存在 直接进入页面 
		/*if($user_info){
		    $http = 'http://'.$_SERVER['SERVER_NAME'].'/wscjf_app/?';
		    header("Location:{$http}");
		    setcookie('openids',$result1['openid'],time()+7200);
		    // setcookie('text3','indexs3',time()+7200);
		}*/
		if(empty($user_info)){
			$http_url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$result1['access_token']."&openid=".$result1['openid']."&lang=zh_CN ";
			$result = $this->curl_get($http_url);

			if(empty($result1['openid'])){
				// setcookie('text6','indexs6',time()+7200);
				echo '网络异常！！！！！';exit();
			}
			//dump($result);
			//查询地域   判断城市排序
			$ar=M('areas');
			$data2['area_name'] = $result['city'];
			$area=$ar->field('area_id,parent_id')->where($data2)->find();
			//dump($area);die;
			//dump($result);die;
			setcookie('openids',$result1['openid'],time()+7200);
			$data['openid'] = $result['openid'];
			$data['nickname'] = $this->emoji_encode($result['nickname']);
			$data['sex'] = $result['sex'];
			$data['city'] = $result['city'];
			$data['province'] = $result['province'];
			$data['headimg'] = $result['headimgurl'];
			$data['country'] = $result['country'];
			$data['add_time'] = time();
			$data['area_sheng'] =$area['parent_id'] ;
			$data['area_shi'] =$area['area_id'] ;
			//dump($data);die;
			$re = M("Member")->add($data);
			if(empty($fx)){
				$http = 'http://'.$_SERVER['SERVER_NAME'].'/wscjf_app/index.html';
			}else{
				$http = 'http://'.$_SERVER['SERVER_NAME'].'/wscjf_app/index.html?from=singlemessage#/haoxingdi?order_id='.$order_id;
			}
            $arr=cookie('arr');
            //dump($arr);die();
            if(!empty( $arr)){
               $http='http://'.$_SERVER['SERVER_NAME'].'/wscjf_app'.'#/haoxingdi?order_id='+cookie('order_id');

                header("Location:".$arr);
            }else{
                header("Location:".$http);
            }
			/*if($ar){

			    dump(11);die();
                header("Location:".$arr);
            }else{
                header("Location:".$http);
            }*/
			//dump($http);die;

			//先将信息存储
			// $GLOBALS['open']=$result['openid'];
		
		}else{

			setcookie('openids',$result1['openid'],time()+7200);
			//入在次登陆检查头像没有更换 更改头像
		    $http_url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$result1['access_token']."&openid=".$result1['openid']."&lang=zh_CN ";
		    $result = $this->curl_get($http_url);
		    // setcookie('text5','indexs5',time()+7200);
		    //dump($result);die;
		    if(empty($result1['openid'])){
		        echo '网络异常！！！';exit();
		    }
		    //查询地域   判断城市排序
			$ar=M('areas');
			$data2['area_name'] = $result['city'];
			$area=$ar->field('area_id,parent_id')->where($data2)->find();
		    //先将信息存储
		    $data['openid'] = $result['openid'];
		    // dump($data);
		   // $e = M("Member")->where($data)->find();
		    $data1['nickname'] = $this->emoji_encode($result['nickname']);
			$data1['sex'] = $result['sex'];
			$data1['city'] = $result['city'];
			$data1['province'] = $result['province'];
			$data1['headimg'] = $result['headimgurl'];
			$data1['country'] = $result['country'];
			$data1['area_sheng'] =$area['parent_id'] ;
			$data1['area_shi'] =$area['area_id'] ;
			//dump($data);die;
			//dump($data1);die;
			//dump($area);die;
			$re = M("Member")->where($data)->save($data1);
		    if(empty($fx)){
				$http = 'http://'.$_SERVER['SERVER_NAME'].'/wscjf_app/index.html';
			}else{
				$http = 'http://'.$_SERVER['SERVER_NAME'].'/wscjf_app/index.html?from=singlemessage#/haoxingdi?order_id='.$order_id;	
			}


            if(cookie('my_cnt')==9){
              //  dump(11);die();
                $arr='http://'.$_SERVER['SERVER_NAME'].'/wscjf_app/index.html#/haoxingdi?order_id='.cookie('order_id');
                //dump($arr);die();
                header("Location:".$arr);
                die();
            }else{
                header("Location:".$http);
                die();
            }
		}
		// $this->ajaxReturn(['status'=>1,'openid'=>$result['openid'],'msg'=>'系统繁忙，请稍后再试'],'JSONP');

		  $this->display();
     }
      public function test(){
           $a = Vendor('WxPays.lib.test');
      // var_dump($a);exit();
        $appid = $this->AppID;
        $secret = $this->AppSecret;
        $jssdk = new \JSSDK($appid, $secret);
        //返回签名基本信息
        $signPackage = $jssdk->getSignPackage();
        var_dump($signPackage);exit();
	
     }

     //去除昵称表情
     private function emoji_encode($nickname){
        $strEncode = '';
        $length = mb_strlen($nickname,'utf-8');
        for ($i=0; $i < $length; $i++) {
            $_tmpStr = mb_substr($nickname,$i,1,'utf-8');
            if(strlen($_tmpStr) >= 4){
                $strEncode .= '';
            }else{
                $strEncode .= $_tmpStr;
            }
        }
        return $strEncode;
    }

}
